using System;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using Send2Sharepoint.Common;
using Send2Sharepoint.Excel;
using Send2Sharepoint.Forms;
using Icon = System.Drawing.Icon;

namespace Send2Sharepoint
{
    public partial class Connect
    {
        CommandBarButton _uploadExcelDocumentButton;

        public void RemoveExcel()
        {
            _uploadExcelDocumentButton.Delete(Missing.Value);
            _uploadExcelDocumentButton = null;
        }

        private void CreateExcelButtons(CommandBars bars)
        {
            var standardBar = bars["Standard"];
            try
            {
                _uploadExcelDocumentButton = (CommandBarButton)standardBar.Controls[_rm.GetString("ApplicationName")];
                _uploadExcelDocumentButton.Delete(true);
            }
            catch
            {
                //nothing..
            }
            _uploadExcelDocumentButton = (CommandBarButton)standardBar.Controls.Add(1,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value);
            _uploadExcelDocumentButton.Caption = _rm.GetString("ApplicationName");
            _uploadExcelDocumentButton.Style = MsoButtonStyle.msoButtonIconAndCaption;
            _uploadExcelDocumentButton.Tag = "UploadExcelDocumentButton";

            var icon = (Icon)_rm.GetObject("sharepoint");
            var bmp = icon.ToBitmap();
            Clipboard.SetDataObject(bmp, true);
            _uploadExcelDocumentButton.FaceId = 0;
            _uploadExcelDocumentButton.PasteFace();
            _uploadExcelDocumentButton.Click += UploadExcelDocumentButton_Click;
        }

        public void InitializeExcel()
        {
            var thisApp = (_Application)Globals.ThisApplication;
            try
            {
                var commandBars = thisApp.CommandBars;
                CreateExcelButtons(commandBars);
            }
            catch (System.Exception ex)
            {
                Logging.WriteException(ex);
                throw;
            }
           
        }

        void UploadExcelDocumentButton_Click(CommandBarButton ctrl, ref bool cancelDefault)
        {
            try
            {
                var workbook = ((_Application) Globals.ThisApplication).ActiveWorkbook;
                if (!string.IsNullOrEmpty(workbook.Path))
                {
                    var uploadDocument = new UploadExcelDocument(workbook)
                                             {
                                                 Description = _rm.GetString("ApplicationName") + workbook.Name
                                             };

                    _indexDocument = new IndexDocument(uploadDocument)
                                         {
                                             MinimizeBox = false,
                                             StartPosition = FormStartPosition.CenterScreen
                                         };
                    _indexDocument.ShowDialog();
                }
                else
                {
                    MessageBox.Show(string.Format(_rm.GetString("NotSavedMessage"), "Workbook"),
                                    _rm.GetString("ApplicationName"), MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception ex)
            {
                Logging.WriteException(ex);
                throw;
            }
        }
    }
}
